/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rafkind.raytracer;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 *
 * @author drafkind
 */
public class Ray {
  private Vector3 position;
  private Vector3 direction;
  private Color color;

  private Ray(Vector3 position, Vector3 direction) {
    this.color = Color.BLACK;
    this.position = position;
    this.direction = direction;
  }

  public static Ray createPositionally(Vector3 from, Vector3 to) {
    Vector3 diff = to.minus(from);
    return new Ray(from, diff.getNormalized());
  }
  
  public Color getColor() {
    return color;
  }

  public void setColor(Vector3 rgb) {
    color = new Color((float)rgb.getData()[0], 
            (float)rgb.getData()[1],
            (float)rgb.getData()[2]);
  }

  public void translateAlong(double d) {
    position = position.plus(direction.times(d));
  }

  public Vector3 getPosition() {
    return position;
  }

  public Vector3 getDirection() {
    return direction;
  }

  public List<IntersectionPoint> shootAt(List<VisibleObject> visibleObjects) {
    List<IntersectionPoint> rayObjectIntersections =
            new ArrayList<IntersectionPoint>();
    for (VisibleObject vo : visibleObjects) {
      for (IntersectionPoint p : vo.getIntersectionPoints(this)) {
        rayObjectIntersections.add(p);
      }
    }
    Collections.sort(rayObjectIntersections);

    return rayObjectIntersections;
  }
}
